<template>
  <div class="ele-body">
    <!-- 搜索表单 -->
    <div class="block-interval">
      <a-card :bordered="false">
        <a-form layout="inline" :model="where">
          <a-row>
            <a-form-item label="所属牛能牧场:" name="ranchId">
              <a-select
                show-search
                v-model:value="where.ranchId"
                placeholder="请选择所属牛能牧场"
                style="width: 205px;"
                :show-arrow="false"
                :filter-option="false"
                :not-found-content="null"
                allow-clear
              >
                <a-select-option v-for="item in ranchList" :key="item.ranchId" >
                  {{ item.ranchName }}
                </a-select-option>
              </a-select>
            </a-form-item>

            <a-form-item label="所属牧云汇牧场:" name="farmId">
              <a-select
                show-search
                v-model:value="where.farmId"
                placeholder="请选择所属牧云汇牧场"
                style="width: 205px;"
                :show-arrow="false"
                :filter-option="false"
                :not-found-content="null"
                allow-clear
              >
                <a-select-option v-for="item in farmList" :key="item.farmId" >
                  {{ item.name }}
                </a-select-option>
              </a-select>
            </a-form-item>
            <a-form-item class="ele-text-center">
              <a-space>
                <a-button type="primary" @click="reload">查询</a-button>
                <a-button @click="reset">重置</a-button>
              </a-space>
            </a-form-item>
			
          </a-row>
        </a-form>
      </a-card>
    </div>

    <!-- 表格 -->
    <div>
      <a-card :bordered="false">
        <ele-pro-table
          ref="table"
          row-key="configId"
          :datasource="datasource"
          :columns="columns"
          :where="where"
          :scroll="{ x: 'max-content' }"
        >
          <!-- table上边工具栏 -->
          <template #toolbar>
            <a-space>
              <a-button type="primary" @click="openEdit()">
                <template #icon>
                  <plus-outlined />
                </template>
                <span>新建</span>
              </a-button>
            </a-space>
          </template>

           <template #bodyCell="{ column, record }">
            <!-- table操作栏按钮 -->
            <template v-if="column.key === 'action'">
              <a-space>
                <a @click="openEdit(record)">修改</a>
                <a-divider type="vertical" />
                <a @click="openAllocation(record)">关联的牛舍</a>
                <a-divider type="vertical" />
                <a-popconfirm title="确定要删除此记录吗？" @confirm="remove(record)">
                  <a class="ele-text-danger">删除</a>
                </a-popconfirm>
              </a-space>
            </template>
          </template>
        </ele-pro-table>
      </a-card>
    </div>
    <DeviceManagerdeviceAiedgeComputerHazzxConfigEdit v-if="showEdit" v-model:visible="showEdit" :data="current" :scene="'config'" @done="reload" />
    <DeviceManagerdeviceAiedgeComputerHazzxConfigAllocation v-if="showAllocation" v-model:visible="showAllocation" :data="current" @done="reload" />
  </div>
</template>

<script>
import { PlusOutlined } from '@ant-design/icons-vue';
import { DeviceAiEdgeSprayConfigApi } from '@/api/property/aiedgecomputer/DeviceAiEdgeSprayConfigApi';
import { DeviceAiEdgeFarmApi } from '@/api/property/aiedgecomputer/DeviceAiEdgeFarmApi';
import { RanchApi } from '@/api/property/RanchApi';
import DeviceManagerdeviceAiedgeComputerHazzxConfigEdit from './hazzxconfig-edit.vue'
import DeviceManagerdeviceAiedgeComputerHazzxConfigAllocation from './hazzxconfig-allocation.vue'
import { message } from 'ant-design-vue';
export default {
  name: 'DeviceManagerdeviceAiedgeComputerHazzxConfigList',
  components: {
    PlusOutlined,
    DeviceManagerdeviceAiedgeComputerHazzxConfigEdit,
    DeviceManagerdeviceAiedgeComputerHazzxConfigAllocation
  },
  async mounted() {
  },
  async created() {
    let that = this;
    RanchApi.list({}).then(resp => {
      let _ranchList = (resp ? resp.data : null) || [];
      that.ranchList = _ranchList;
    });

    DeviceAiEdgeFarmApi.list({}).then(resp => {
      let _farmList = (resp ? resp.data : null) || [];
      that.farmList = _farmList;
    });
  },
  data() {
    return {
      // 表格列配置
      columns: [
        {
          title: '配置ID',
          dataIndex: 'configId'
        },
        {
          title: '配置',
          dataIndex: 'configName',
          width : 200
        },
        {
          title: '是否牧场默认配置',
          dataIndex: 'defaultt',
          customRender: function(row) {
            if (row.text === 1) {
              return '是';
            } 
            return '否';  
          },
          width : 80
        },
        {
          title: '是否牧场默认配置',
          dataIndex: 'farmDefault',
          customRender: function(row) {
            if (row.text === 1) {
              return '是';
            } 
            return '否';  
          },
           width : 80
        },
        {
          title: '所属牧云汇牧场牧场',
          dataIndex: 'farmName'
        },
        {
          title: '所属牛能牧场',
          dataIndex: 'ranchName'
        },
        {
          title: '操作',
          key: 'action',
          width: 200,
          align: 'center',
        }
      ],
      // 表格搜索条件
      where: {},
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      ranchList : [],
      farmList : [],
      showAllocation : false,
    };
  },
  methods: {
    /**
     * 搜索按钮
     *
     * @author cancan
     * @date 2025/06/28 19:48
     */
    reload() {
      this.selection = [];
      this.$refs.table.reload({ page: 1 });
    },

    /**
     * 重置搜索
     *
     * @author cancan
     * @date 2025/06/28 19:48
     */
    reset() {
      this.where.ranchId = '';
      this.where.farmId = '';
      this.$nextTick(() => {
        this.reload();
      });
    },

    /**
     * 删除
     *
     * @author cancan
     * @date 2025/06/28 19:48
     */
    async remove(row) {
      const result = await DeviceAiEdgeSprayConfigApi.delete({ configId: row.configId, farmId: row.farmId });
      message.success(result.message);
      this.reload();
    },

    /**
     * 打开新增或编辑弹窗
     *
     * @author cancan
     * @date 2025/06/28 19:48
     */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },

    openAllocation(row) {
      this.current = row;
      this.showAllocation = true;
    },

    datasource({ page, limit, where, orders }) {
      return DeviceAiEdgeSprayConfigApi.findPage({ ...where, ...orders, pageNo: page, pageSize: limit });
    },
  }
};
</script>